package com.hn.project.rqinspect.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.hn.common.constant.RedisConstant;
import com.hn.common.enums.*;
import com.hn.common.exception.BusinessException;
import com.hn.common.utils.Common;
import com.hn.common.utils.CommonUtils;
import com.hn.common.utils.SecurityUtils;
import com.hn.common.utils.sql.SqlUtil;
import com.hn.project.rqinspect.domain.database.TaskCheckChangeRecord;
import com.hn.project.rqinspect.domain.database.TaskCheckRecord;
import com.hn.project.rqinspect.domain.database.TaskRecord;
import com.hn.project.rqinspect.domain.dto.*;
import com.hn.project.rqinspect.domain.vo.QuesCheckRecordVo;
import com.hn.project.rqinspect.domain.vo.TaskCheckChangeRecordVo;
import com.hn.project.rqinspect.domain.vo.TaskCheckRecordVo;
import com.hn.project.rqinspect.mapper.*;
import com.hn.project.rqinspect.service.TaskCacheService;
import com.hn.project.rqinspect.service.TaskCheckService;
import com.hn.utils.AssertUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaskCheckServiceImpl implements TaskCheckService {

    @Autowired
    private TaskCheckRecordMapper taskCheckRecordMapper;

    @Autowired
    private TaskRecordMapper taskRecordMapper;

    @Autowired
    private TaskCheckChangeRecordMapper taskCheckChangeRecordMapper;

    @Autowired
    private UserBranchMapper userBranchMapper;

    @Autowired
    private BranchCheckpointMapper branchCheckpointMapper;

    @Autowired
    private QuesPartItemMapper quesPartItemMapper;

    @Autowired
    private TaskCacheService taskCacheService;

    @Autowired
    private SysUserMapper userMapper;

    @Value("${upload.web.url}")
    private String uploadWebUrl;

    @Override
    public List<TaskCheckRecordDto> listCheckRecordDto(TaskCheckRecordVo taskCheckRecordVo) {
        List<TaskCheckRecordDto> taskCheckRecordDtos = taskCheckRecordMapper.listCheckRecordDto(taskCheckRecordVo);
        for (TaskCheckRecordDto taskCheckRecordDto : taskCheckRecordDtos) {
            String imgUrls = taskCheckRecordDto.getImgUrls();
            taskCheckRecordDto.setImgUrls(CommonUtils.pingJieUrls(imgUrls, uploadWebUrl));
        }
        return taskCheckRecordDtos;
    }

    @Override
    public int addCheckRecord(TaskCheckRecord taskCheckRecord) {
        // 查询当前检查点是否存在检查记录
        Integer taskRecordId = taskCheckRecord.getTaskRecordId();
        AssertUtils.notNull(taskCheckRecord, "请选择巡检任务");
        String branchCheckpointId = taskCheckRecord.getBranchCheckpointId();
        AssertUtils.notNull(branchCheckpointId, "请选择检查点");

        TaskCheckRecord qTaskCheckRecord = new TaskCheckRecord();
        qTaskCheckRecord.setTaskRecordId(taskRecordId);
        qTaskCheckRecord.setBranchCheckpointId(branchCheckpointId);
        TaskCheckRecord existTaskCheckRecord = taskCheckRecordMapper.selectOne(qTaskCheckRecord);
        if (existTaskCheckRecord != null) {
            throw new BusinessException("当前检查点已存在检查记录");
        }

        Integer commentType = taskCheckRecord.getCommentType();
        if (CommentTypeEnum.GOOD.type.equals(commentType)) {
            // 如果是好评,一个部位只能加一个
            if (branchCheckpointId.split(",").length > 1) {
                throw new BusinessException("好评不可以选择多个检查点");
            }

            // 查询当前部位有没有评价过
            Integer branchPartId = taskCheckRecord.getBranchPartId();
            existTaskCheckRecord = taskCheckRecordMapper.getCheckRecord(taskRecordId, branchPartId);
            if (existTaskCheckRecord != null) {
                throw new BusinessException("当前部位已存在好评");
            }

            String checkPartItems = taskCheckRecord.getCheckPartItems();
            String commentContent = quesPartItemMapper.getContent("符合", checkPartItems);
            taskCheckRecord.setCommentContent(commentContent);
        } else if (CommentTypeEnum.BAD.type.equals(commentType)) {
            if (branchCheckpointId.split(",").length > 1) {
                throw new BusinessException("差评不可以选择多个检查点");
            }

            String checkPartItems = taskCheckRecord.getCheckPartItems();
            Integer riskEvaluation = quesPartItemMapper.getRiskEvaluation(checkPartItems);
            taskCheckRecord.setRiskEvaluation(riskEvaluation);

            String commentContent = quesPartItemMapper.getContent("不符合", checkPartItems);
            taskCheckRecord.setCommentContent(commentContent);
            taskCheckRecord.setFollowStatus(FollowStatusEnum.CHANGE.status);
        }

        String address = branchCheckpointMapper.getAddressByCheckpointIds(branchCheckpointId.split(","));
        taskCheckRecord.setAddress(address);
        taskCheckRecord.setSubmitStatus(SubmitStatusEnum.NO.status);
        return taskCheckRecordMapper.save(taskCheckRecord);
    }

    @Override
    public int updateCheckRecord(TaskCheckRecord taskCheckRecord) {
        Integer id = taskCheckRecord.getId();

        // 如果是好评,一个部位只能加一个
        Integer commentType = taskCheckRecord.getCommentType();
        if (CommentTypeEnum.GOOD.type.equals(commentType)) {
            // 查询当前部位有没有评价过
            Integer branchPartId = taskCheckRecord.getBranchPartId();
            Integer taskRecordId = taskCheckRecord.getTaskRecordId();
            TaskCheckRecord existTaskCheckRecord = taskCheckRecordMapper.getCheckRecord(taskRecordId, branchPartId);
            if (existTaskCheckRecord != null && !existTaskCheckRecord.getId().equals(id)) {
                throw new BusinessException("当前部位已存在好评");
            }

            String checkPartItems = taskCheckRecord.getCheckPartItems();
            String commentContent = quesPartItemMapper.getContent("符合", checkPartItems);
            taskCheckRecord.setCommentContent(commentContent);
        } else if (CommentTypeEnum.BAD.type.equals(commentType)) {
            // 查询当前检查点是否存在差评
            TaskCheckRecord qTaskCheckRecord = new TaskCheckRecord();
            qTaskCheckRecord.setTaskRecordId(taskCheckRecord.getTaskRecordId());
            qTaskCheckRecord.setBranchCheckpointId(taskCheckRecord.getBranchCheckpointId());
            TaskCheckRecord existTaskCheckRecord = taskCheckRecordMapper.selectOne(qTaskCheckRecord);
            if (existTaskCheckRecord != null && !existTaskCheckRecord.getId().equals(taskCheckRecord.getId())) {
                throw new BusinessException("当前检查点已存在检查记录");
            }

            String checkPartItems = taskCheckRecord.getCheckPartItems();
            Integer riskEvaluation = quesPartItemMapper.getRiskEvaluation(checkPartItems);
            taskCheckRecord.setRiskEvaluation(riskEvaluation);

            String commentContent = quesPartItemMapper.getContent("不符合", checkPartItems);
            taskCheckRecord.setCommentContent(commentContent);
            taskCheckRecord.setFollowStatus(FollowStatusEnum.CHANGE.status);
        }
        String branchCheckpointId = taskCheckRecord.getBranchCheckpointId();
        String address = branchCheckpointMapper.getAddressByCheckpointIds(branchCheckpointId.split(","));
        taskCheckRecord.setAddress(address);
        taskCheckRecord.setUpdateTime(DateUtil.date());
        return taskCheckRecordMapper.updateByPrimaryKeySelective(taskCheckRecord);
    }

    @Override
    public TaskCheckRecordDto getCheckRecordDto(Integer checkRecordId) {
        TaskCheckRecordDto checkRecordDto = taskCheckRecordMapper.getCheckRecordDto(checkRecordId);
        if (checkRecordDto != null) {
            String imgUrls = checkRecordDto.getImgUrls();
            checkRecordDto.setImgUrls(CommonUtils.pingJieUrls(imgUrls, uploadWebUrl));

            if (CommentTypeEnum.BAD.type.equals(checkRecordDto.getCommentType())) {
                TaskCheckChangeRecord checkChangeRecord =
                        taskCheckChangeRecordMapper.getChangeRecordByCheckRecordId(checkRecordId, AuditStatusEnum.SUCCESS.status);
                if (checkChangeRecord != null) {
                    String imgUrl = checkChangeRecord.getImgUrl();
                    checkChangeRecord.setImgUrl(CommonUtils.pingJieUrls(imgUrl, uploadWebUrl));
                    checkRecordDto.setCheckChangeRecord(checkChangeRecord);
                }
            }
        }
        return checkRecordDto;
    }

    @Override
    public int delCheckRecord(Integer checkRecordId) {
        return taskCheckRecordMapper.deleteByPrimaryKey(checkRecordId);
    }

    @Override
    public int delByTaskRecordId(Integer taskRecordId) {
        TaskCheckRecord checkRecord = new TaskCheckRecord();
        checkRecord.setTaskRecordId(taskRecordId);
        return taskCheckRecordMapper.deleteByExample(SqlUtil.newExample(checkRecord));
    }

    @Override
    public int addChangeRecord(TaskCheckChangeRecord checkChangeRecord) {
        // 查询是否已经提交了整改记录
        Integer checkRecordId = checkChangeRecord.getCheckRecordId();
        TaskCheckChangeRecord exist =
                taskCheckChangeRecordMapper.getChangeRecordByCheckRecordId(checkRecordId, AuditStatusEnum.AUDIT.status);
        if (exist != null) {
            throw new BusinessException("已提交整改记录");
        }
        checkChangeRecord.setAuditStatus(0);
        int rows = taskCheckChangeRecordMapper.save(checkChangeRecord);


        TaskCheckRecord checkRecord = taskCheckRecordMapper.selectByPrimaryKey(checkRecordId);
        if (checkRecord == null) {
            throw new BusinessException("检查记录不存在");
        }

        // 查询网点的系统管理员
        Integer taskRecordId = checkRecord.getTaskRecordId();
        TaskRecord taskRecord = taskRecordMapper.selectByPrimaryKey(taskRecordId);
        Integer branchId = taskRecord.getBranchId();
        List<Integer> userIds = userBranchMapper.listUserIdsByBranchId(branchId, RoleIdEnum.ADMIN.id);
        for (Integer userId : userIds) {
            taskCacheService.add(RedisConstant.CHECK_CHANGE_APPLY, userId, checkRecordId);
        }

        return rows;
    }

    @Override
    @Transactional
    public int auditChangeRecord(TaskCheckChangeRecordVo taskCheckChangeRecordVo) {
        Integer auditStatus = taskCheckChangeRecordVo.getAuditStatus();
        Integer id = taskCheckChangeRecordVo.getId();
        AssertUtils.notNull(id, "请选择您要审核的整改记录");
        AssertUtils.notNull(auditStatus, "请选择通过或者拒绝");

        TaskCheckChangeRecord existCheckChangeRecord = taskCheckChangeRecordMapper.selectByPrimaryKey(id);
        AssertUtils.notNull(existCheckChangeRecord, "整改记录不存在");

        if (!AuditStatusEnum.AUDIT.status.equals(existCheckChangeRecord.getAuditStatus())) {
            throw new BusinessException("请勿重复审核");
        }

        TaskCheckChangeRecord updateCheckChangeRecord = new TaskCheckChangeRecord();
        updateCheckChangeRecord.setId(id);
        updateCheckChangeRecord.setAuditStatus(taskCheckChangeRecordVo.getAuditStatus());
        updateCheckChangeRecord.setUpdateTime(DateUtil.date());
        updateCheckChangeRecord.setAuditTime(DateUtil.date());
        updateCheckChangeRecord.setAuditUserId(SecurityUtils.getUserId());
        int rows = taskCheckChangeRecordMapper.updateByPrimaryKeySelective(updateCheckChangeRecord);

        Integer checkRecordId = existCheckChangeRecord.getCheckRecordId();
        TaskCheckRecord taskCheckRecord = taskCheckRecordMapper.selectByPrimaryKey(checkRecordId);
        if (AuditStatusEnum.SUCCESS.status.equals(auditStatus)) {
            taskCheckRecord.setFollowStatus(1);
            taskCheckRecord.setCompleteTime(DateUtil.date());
            taskCheckRecord.setCompleteUserId(existCheckChangeRecord.getUserId());
            taskCheckRecord.setUpdateTime(DateUtil.date());
            taskCheckRecordMapper.updateByPrimaryKeySelective(taskCheckRecord);

            taskCacheService.add(RedisConstant.CHECK_CHANGE_APPLY_SUCCESS, taskCheckRecord.getUserId(), checkRecordId);

            // 给对应网点的人发送信息


        } else {
            // 查询网点负责人
            TaskRecord taskRecord = taskRecordMapper.selectByPrimaryKey(taskCheckRecord.getTaskRecordId());
            Integer branchId = taskRecord.getBranchId();
            List<Integer> userIds = userBranchMapper.listUserIdsByBranchId(branchId, RoleIdEnum.ADMIN.id);
            for (Integer userId : userIds) {
                taskCacheService.add(RedisConstant.CHECK_CHANGE_APPLY_FAIL, userId, checkRecordId);
            }
        }
        taskCacheService.delete(RedisConstant.CHECK_CHANGE_APPLY, checkRecordId);
        return rows;
    }

    @Override
    public List<TaskQuesCheckRecordDto> listAuditCheckRecord(Integer userId, Integer auditStatus) {
        // 获取当前用户管理的网点
        List<Integer> branchIds = userBranchMapper.listBranchIdsByUserId(userId);
        if (branchIds == null || branchIds.isEmpty()) {
            return null;
        }
        return taskCheckRecordMapper.listAuditCheckRecord(branchIds, auditStatus);
    }

    @Override
    public TaskQuesCheckRecordDetailDto checkRecordDetail(Integer checkRecordId) {
        Integer userId = SecurityUtils.getUserId();
        TaskQuesCheckRecordDetailDto taskQuesCheckRecordDetailDto = taskCheckRecordMapper.checkRecordDetail(checkRecordId);
        if (taskQuesCheckRecordDetailDto != null) {
            String imgUrls = taskQuesCheckRecordDetailDto.getImgUrls();
            taskQuesCheckRecordDetailDto.setImgUrls(CommonUtils.pingJieUrls(imgUrls, uploadWebUrl));

            List<TaskCheckChangeRecord> checkChangeRecords = taskCheckChangeRecordMapper.listCheckChangeRecord(checkRecordId);
            if (checkChangeRecords != null && !checkChangeRecords.isEmpty()) {
                TaskCheckChangeRecord checkChangeRecord = checkChangeRecords.get(0);
                Integer auditStatus = checkChangeRecord.getAuditStatus();
                taskQuesCheckRecordDetailDto.setAuditStatus(auditStatus);

                for (TaskCheckChangeRecord changeRecord : checkChangeRecords) {
                    String imgUrl = changeRecord.getImgUrl();
                    changeRecord.setImgUrl(CommonUtils.pingJieUrls(imgUrl, uploadWebUrl));
                }
            }
            taskQuesCheckRecordDetailDto.setChangeRecordList(checkChangeRecords);

            Integer exist = taskCacheService.get(RedisConstant.CHECK_CHANGE_QUES, userId, taskQuesCheckRecordDetailDto.getCheckRecordId());
            if (exist != null && exist > 0) {
                taskQuesCheckRecordDetailDto.setReadFlag(1);
            }

            String otherUserIds = taskQuesCheckRecordDetailDto.getOtherUserIds();
            String checkUserName = taskQuesCheckRecordDetailDto.getCheckUserName();
            if (StrUtil.isNotBlank(otherUserIds)) {
                String userNames = userMapper.getUserNamesByUserIds(otherUserIds);
                checkUserName = checkUserName + "," + userNames;
                taskQuesCheckRecordDetailDto.setCheckUserName(checkUserName);
            }
        }
        return taskQuesCheckRecordDetailDto;
    }

    @Override
    public List<QuesCheckRecordDto> listQuesCheckRecord(Integer branchId) {
        List<Integer> branchIds = new ArrayList<>();
        branchIds.add(branchId);
        return taskCheckRecordMapper.listQuesCheckRecord(null, branchIds, null, FollowStatusEnum.CHANGE.status);
    }

    @Override
    public QuesCheckRecordNumDto countQuesCheckRecord(Integer type, Integer taskType) {
        QuesCheckRecordVo quesCheckRecordVo = new QuesCheckRecordVo();
        QuesCheckRecordNumDto quesCheckRecordNumDto = new QuesCheckRecordNumDto();
        Integer userId = SecurityUtils.getUserId();
        if (RoleTypeEnum.BRANCH.type.equals(type)) {
            List<Integer> branchIds = userBranchMapper.listBranchIdsByUserId(SecurityUtils.getUserId());
            if (branchIds == null || branchIds.isEmpty()) {
                return quesCheckRecordNumDto;
            }
            quesCheckRecordVo.setBranchIds(branchIds);
        } else {
            quesCheckRecordVo.setUserId(userId);
        }

        quesCheckRecordVo.setTaskType(taskType);
        quesCheckRecordVo.setFollowStatus(FollowStatusEnum.SUCCESS.status);
        Integer successNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);

        quesCheckRecordVo.setFollowStatus(FollowStatusEnum.CHANGE.status);
        Integer changeNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);

        quesCheckRecordNumDto.setSuccessChangeNum(successNum);
        quesCheckRecordNumDto.setChangeNum(changeNum);

        int count = taskCacheService.count(RedisConstant.CHECK_CHANGE_APPLY_SUCCESS, userId);
        quesCheckRecordNumDto.setSuccessChangeRemind(count);

        count = taskCacheService.count(RedisConstant.CHECK_CHANGE_APPLY_FAIL, userId);
        quesCheckRecordNumDto.setChangeRemind(count);

        quesCheckRecordVo.setAuditStatus(AuditStatusEnum.FAIL.status);
        Integer failChangeNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);
        quesCheckRecordNumDto.setFailChangeNum(failChangeNum);
        return quesCheckRecordNumDto;
    }

    @Override
    public List<QuesCheckRecordDto> listQuesCheckRecord(Integer type, Integer taskType, Integer followStatus) {
        Integer userId = null;
        List<Integer> branchIds = null;
        if (RoleTypeEnum.BRANCH.type.equals(type)) {
            branchIds = userBranchMapper.listBranchIdsByUserId(SecurityUtils.getUserId());
            if (branchIds == null || branchIds.isEmpty()) {
                return new ArrayList<>();
            }
        } else {
            userId = SecurityUtils.getUserId();
        }
        List<QuesCheckRecordDto> quesCheckRecordDtos = taskCheckRecordMapper.listQuesCheckRecord(userId, branchIds, taskType, followStatus);

        if (RoleTypeEnum.BRANCH.type.equals(type)) {
            for (QuesCheckRecordDto quesCheckRecordDto : quesCheckRecordDtos) {
                Integer checkRecordId = quesCheckRecordDto.getId();
                Integer exist = taskCacheService.get(RedisConstant.CHECK_CHANGE_QUES, userId, checkRecordId);
                if (exist != null && exist > 0) {
                    quesCheckRecordDto.setReadFlag(1);
                }

                // 判断是否待审核
                TaskCheckChangeRecord taskCheckChangeRecord =
                        taskCheckChangeRecordMapper.getChangeRecordByCheckRecordId(checkRecordId, null);
                if (taskCheckChangeRecord != null) {
                    Integer auditStatus = taskCheckChangeRecord.getAuditStatus();
                    Integer status = AuditStatusEnum.AUDIT.status.equals(auditStatus) ? 1 : 2;
                    quesCheckRecordDto.setStatus(status);
                }
            }
        } else {
            for (QuesCheckRecordDto quesCheckRecordDto : quesCheckRecordDtos) {
                Integer checkRecordId = quesCheckRecordDto.getId();
                Integer exist = taskCacheService.get(RedisConstant.CHECK_CHANGE_APPLY_SUCCESS, userId, checkRecordId);
                if (exist != null && exist > 0) {
                    quesCheckRecordDto.setReadFlag(1);
                }
            }
        }

        return quesCheckRecordDtos;
    }

    @Override
    public CountCheckMemo countCheckMemo(Integer type, Integer userId) {
        CountCheckMemo countCheckMemo = new CountCheckMemo();
        if (RoleTypeEnum.GANBU.type.equals(type)) {
            // 巡检干部 ,整改问题新进展->审核通过的
            int count = taskCacheService.count(RedisConstant.CHECK_CHANGE_APPLY_SUCCESS, userId);
            countCheckMemo.setCheckRemindNum(count);
        } else if (RoleTypeEnum.BRANCH.type.equals(type)) {
            // 网点负责人 , 新出的问题
//            int changeQuesNum = taskCacheService.count(RedisConstant.CHECK_CHANGE_QUES, userId);
            List<Integer> branchIds = userBranchMapper.listBranchIdsByUserId(userId);
            if (branchIds == null || branchIds.isEmpty()) {
                return countCheckMemo;
            }

            QuesCheckRecordVo quesCheckRecordVo = new QuesCheckRecordVo();
            quesCheckRecordVo.setBranchIds(branchIds);
            quesCheckRecordVo.setFollowStatus(FollowStatusEnum.CHANGE.status);
            int changeQuesNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);
            countCheckMemo.setCheckBranchQuesNum(changeQuesNum);

            // 待整改未审核通过
//            int auditFailNum = taskCacheService.count(RedisConstant.CHECK_CHANGE_APPLY_FAIL, userId);
            quesCheckRecordVo.setAuditStatus(AuditStatusEnum.FAIL.status);
            int auditFailNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);
            countCheckMemo.setAuditFailNum(auditFailNum);
        } else {
            // 系统管理员 ,整改审核申请
            int count = taskCacheService.count(RedisConstant.CHECK_CHANGE_APPLY, userId);
            countCheckMemo.setChangeApplyNum(count);
            // 平台已累计完成巡检个数
            List<Integer> branchIds = userBranchMapper.listBranchIdsByUserId(userId);
            if (branchIds == null || branchIds.isEmpty()) {
                return countCheckMemo;
            }

            //
            int totalTaskRecordNum = taskRecordMapper.countFinishTaskNumByBranchIds(branchIds, null);
            countCheckMemo.setTotalTaskRecordNum(totalTaskRecordNum);
            // 不合格问题个数
            QuesCheckRecordVo quesCheckRecordVo = new QuesCheckRecordVo();
            quesCheckRecordVo.setBranchIds(branchIds);
            int totalUnQualifiedNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);
            countCheckMemo.setTotalUnQualifiedNum(totalUnQualifiedNum);
            // 已整改问题个数
            quesCheckRecordVo.setFollowStatus(FollowStatusEnum.SUCCESS.status);
            int successChangeNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);
            countCheckMemo.setSuccessChangeNum(successChangeNum);

        }
        return countCheckMemo;
    }

    @Override
    public CountCheckCenter countCheckCenter(Integer type, Integer userId) {
        CountCheckCenter countCheckCenter = new CountCheckCenter();
        if (RoleTypeEnum.GANBU.type.equals(type)) {
            // 巡检干部
            // 已完成
            Integer finishNum = taskRecordMapper.countFinishTaskNumByUserId(userId, null);
            countCheckCenter.setFinishNum(finishNum);
            // 查出的问题
            QuesCheckRecordVo quesCheckRecordVo = new QuesCheckRecordVo();
            quesCheckRecordVo.setUserId(userId);
            Integer checkQueNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);
            countCheckCenter.setCheckQueNum(checkQueNum);
            // 我的草槁
            Integer draftNum = taskRecordMapper.countDraftTaskNumByUserId(userId, null);
            countCheckCenter.setDraftNum(draftNum);
        } else if (RoleTypeEnum.BRANCH.type.equals(type)) {
            // 网点负责人
            List<Integer> branchIds = userBranchMapper.listBranchIdsByUserId(userId);

            // 已完成
            Integer finishNum = taskRecordMapper.countFinishTaskNumByBranchIds(branchIds, null);
            countCheckCenter.setFinishNum(finishNum);
            // 网点的问题
            QuesCheckRecordVo quesCheckRecordVo = new QuesCheckRecordVo();
            quesCheckRecordVo.setBranchIds(branchIds);
            Integer checkQueNum = taskCheckRecordMapper.countQuesCheckRecord(quesCheckRecordVo);
            countCheckCenter.setCheckQueNum(checkQueNum);
            // 我的草稿
            Integer draftNum = taskRecordMapper.countDraftTaskNumByUserId(userId, null);
            countCheckCenter.setDraftNum(draftNum);
        } else {
            // 系统管理员
            // 获取当前用户管理的网点
            List<Integer> branchIds = userBranchMapper.listBranchIdsByUserId(userId);
            if (branchIds == null || branchIds.isEmpty()) {
                return countCheckCenter;
            }

            // 审核通过
            Integer auditSuccessNum = taskCheckRecordMapper.countAuditCheckRecord(branchIds, AuditStatusEnum.SUCCESS.status);
            countCheckCenter.setAuditSuccessNum(auditSuccessNum);
            // 待审核
            Integer auditNum = taskCheckRecordMapper.countAuditCheckRecord(branchIds, AuditStatusEnum.AUDIT.status);
            countCheckCenter.setAuditNum(auditNum);
        }
        return countCheckCenter;
    }
}
